package com.yanyeori.seata.controller;

import com.yanyeori.framework.core.model.WebResponse;
import com.yanyeori.seata.client.ServiceBClient;
import com.yanyeori.seata.dao.BankMapper;
import com.yanyeori.seata.entity.Bank;
import io.seata.core.context.RootContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author chenkuan
 * @version v1.0
 * @desc 测试接口
 * @date 20/8/07 10:29
 */
@RestController
public class TestController implements ServiceBClient {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private BankMapper bankMapper;

    @Override
    public WebResponse rest(String param) {
        String xid = RootContext.getXID();
        logger.info("seata-b被调用，XID={}", xid);
        //操作数据库bank
        Bank bank = new Bank();
        bank.setId(2L);
        bank.setUpdateUser("seata-b");
        bankMapper.updateById(bank);

        /*ExecutorService executorService = Executors.newFixedThreadPool(5);
        executorService.execute(() -> {
            //设置xid
            //RootContext.bind(xid);
            logger.info("多线程处理，XID={}", RootContext.getXID());

            Bank entity = new Bank();
            entity.setId(3L);
            entity.setBankId("TEST_BANK_3");
            entity.setBankName("TEST_BANK_NAME_3");
            entity.setBic("TESTBANK");
            entity.setStatus("4");
            entity.setUpdateUser("seata-b");
            entity.setUpdateTime(System.currentTimeMillis());
            bankMapper.insert(entity);
        });*/

        return new WebResponse<String>().data("seata-b被调用，参数：" + param);
    }
}
